<template>
  <div>
    <TopFilter ref="formPanel" :list="topFilterList" :cols="4" :labelWidth="90" formType="add" columnsRef="xkpjxscxTable" @filterChange="searchSalesPart"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partPartOutboundPartSalesSalesOrder"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :maxHeight="maxHeight"
      height="auto"
      :datakey="'rows'"
      :onEnterEvent="doubleClickFun"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      selectionType="single"
    ></FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button :disabled="onChangeColumn.length != 1" type="primary" @click="openReturn">退 料</el-button>
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" :loading="addBtnState" @click="saveDialog">确 定</el-button>
    </div>
    <BaseDialog :visible.sync="showReturn" title="退料" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ReturnMa :rowData="onChangeColumn[0]" @close="showReturn = false" @saveDialog="saveReturnMa" />
    </BaseDialog>
  </div>
</template>

<script>
import { searchSalesPart, isLocked } from '@/api/part/partOut/partSales/index';
import { getUserId } from '@/assets/js/auth';
import { findByEmployeeId } from '@/api/common/auth/employeeAuth/index';
import { getAllUser } from '@/api/customerRepair/afterSaleVisit/resultEntry';
import { dictionary } from '@/utils/dictMixin';
import ReturnMa from './returnMa';
import { isAuthorized } from '@/utils/index';

export default {
  name: 'salesOrder',
  components: {
    ReturnMa
  },
  mixins: [dictionary],
  props: ['salesNo', 'ptype', 'maxHeight'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: searchSalesPart, // 调用接口查询工单信息
      fetchParams: { xhrAbort: true }, // 调用数据
      columns: this.createTablelist(),
      onChangeColumn: [],
      selectedRows: [],
      addBtnState: null,
      isFinishStatus: 10041002,
      lsit: [],
      serviceAdvisorData: [],
      showReturn: false,
      returnData: []
    };
  },
  mounted() {
    this.getQueryBookingInformation();
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.lsit = [];
    this.initQuery();
  },
  methods: {
    // 行双击事件
    doubleClickFun(row) {
      // 将选中行数据赋值给
      // this.onChangeColumn.push(row);
      // console.log(this.onChangeColumn, '将选中行数据赋值给');

      this.saveDialog();
    },
    initQuery() {
      this.fetchParams.xhrAbort = true;
      // 默认查询未出库已结算
      this.fetchParams = Object.assign({}, { xhrAbort: false }, { salesNo: this.salesNo, isFinish: 10041002, isBalance: 10041001 });
      this.topFilterList.find(x => x.fieldName === 'isFinish').initialValue = 10041002;
      this.topFilterList.find(x => x.fieldName === 'isBalance').initialValue = 10041001;
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '配件销售单号',
          fieldName: 'salesNo',
          initialValue: this.salesNo
        },
        // {
        //   type: 'INPUT',
        //   label: '工单号',
        //   fieldName: 'orderNo'
        // },
        {
          type: 'SELECT',
          label: '是否出库',
          fieldName: 'isFinish',
          // initialValue: 10041002,
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '是否结算',
          fieldName: 'isBalance',
          // initialValue: 10041001,
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '是否厂端下发',
          fieldName: 'isFactory',
          // initialValue: 10041002,
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '是否含工时费',
          fieldName: 'isContainLabour',
          // initialValue: 10041002,
          itemList: this.createDictList(1004)
        }
      ];
    },

    createTablelist() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '配件销售单号',
          dataIndex: 'SALES_PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '单据号',
        //   dataIndex: 'RO_NO',
        //   width: 120,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '客户代码',
          dataIndex: 'CUSTOMER_CODE',
          width: 120,
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'CUSTOMER_NAME',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '销售材料费',
          dataIndex: 'SALES_PART_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true
        },
        // {
        //   title: '结算状态',
        //   dataIndex: 'BALANCE_STATUS',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('1004'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.BALANCE_STATUS, '1004')}</span>;
        //   }
        // },
        {
          title: '锁定人',
          dataIndex: 'LOCK_USER',
          sorter: true,
          dictItem: [],
          showOverflowTooltip: true
        },
        // {
        //   title: '制单人',
        //   dataIndex: 'CREATED_BY',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItmes: [],
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: [],
        //   showOverflowTooltip: true
        // },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '是否厂端下发',
          dataIndex: 'IS_FACTORY',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.IS_FACTORY, '1004')}</span>;
          }
        },
        {
          title: '是否包含工时费',
          dataIndex: 'IS_CONTAIN_LABOUR',
          sorter: true,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.IS_CONTAIN_LABOUR, '1004')}</span>;
          }
        }
      ];
    },

    searchSalesPart(val) {
      console.log(val);
      this.fetchParams = Object.assign({}, val, { xhrAbort: false }, { ptype: this.ptype ? this.ptype : null });
      if (val.isFinish != null) {
        this.isFinishStatus = val.isFinish;
      }
    },
    async getQueryBookingInformation() {
      let res = await getAllUser();
      this.serviceAdvisorData = res.data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
      this.columns.find(x => x.dataIndex === 'LOCK_USER').dictItems = this.serviceAdvisorData;
    },
    getEmployeeName(userId) {
      const item = this.serviceAdvisorData.find(x => {
        return x.value == userId;
      });
      return item ? item.text : '';
    },
    // 查询
    queryClick() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    // 重置
    resetClick() {
      this.$refs.formPanel.RESET_FORM();
    },
    // 选中确定后关闭窗口
    async saveDialog() {
      if (this.onChangeColumn.length) {
        let obj = this.onChangeColumn[0];
        const soNO = obj.SALES_PART_NO;
        // let lock = await getUserId();
        // if (obj.LOCK_USER != null && obj.LOCK_USER != '' && lock != obj.LOCK_USER) {
        //   // 有锁定人弹出提示
        //   const item = this.serviceAdvisorData.find(x => {
        //     return x.value == obj.LOCK_USER;
        //   });
        //   this.returnData = [];
        //   return this.$notify.warning({ title: '提示信息', message: '该单号已被' + item.text + '锁定' });
        // } else {
        // 没有进行单号锁定再传值
        let res = await isLocked(soNO);
        if (res.resultCode == 200) {
          this.$set(this.lsit, 'isFinish', this.isFinishStatus);
          let formdata = Object.assign({}, this.onChangeColumn[0], this.lsit);
          if (this.returnData.length > 0) {
            this.$emit('saveDialog', formdata, this.returnData);
          } else {
            this.$emit('saveDialog', formdata);
          }
        }
        // }
      }
    },
    // 打开退料窗口
    openReturn() {
      if (this.onChangeColumn[0].BALANCE_STATUS != '10041001') {
        this.$notify({
          title: '提示',
          message: '只有已结算的单据可以退料',
          type: 'warning'
        });
        return;
      }
      // 查看当前用户是否有退料权限
      if (isAuthorized('1313', '13131014')) {
        this.showReturn = true;
      } else {
        this.$notify({
          title: '提示',
          message: '当前用户无销售退料权限',
          type: 'warning'
        });
      }
    },
    // 保存退料信息
    saveReturnMa(data) {
      this.returnData = data;
      this.saveDialog();
    },
    // 点击取消关闭窗口
    closeDialog() {
      this.$emit('closeSalesDialog', false);
    }
    // 销售单号查询
  }
};
</script>
<style lang="less" scoped></style>
